// Buffer 类，用来创建一个专门存放二进制数据的缓存区。
const buf =Buffer.from('scitc','utf-8');
console.log(buf.toString('hex'));//7363697463
console.log(buf.toString('utf-8'));//scitc

console.log("-----------------");
// 创建一个长度为 10、且用 0 填充的 Buffer。
const buf2=Buffer.alloc(10);
console.log(buf2);//<Buffer 00 00 00 00 00 00 00 00 00 00>
console.log("-----------------");
// 创建一个长度为 10、且用 0x1 填充的 Buffer。 
const buf3=Buffer.alloc(10,1);
console.log(buf3);//<Buffer 01 01 01 01 01 01 01 01 01 01>
console.log("-----------------");
const buf4=Buffer.allocUnsafe(10);
console.log(buf4);//<Buffer 8e 17 92 13 74 36 34 4c 00 00>
console.log("-----------------");
const buf5=Buffer.from([1,2,3,4,5]);
console.log(buf5);//<Buffer 01 02 03 04 05>
console.log(buf5.toString('hex'));//0102030405
console.log(buf5.toString('hex',0,3));//010203
console.log("-----------------");
const buf6=Buffer.from([0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9]);
const json=JSON.stringify(buf6);
console.log(json);//{"type":"Buffer","data":[1,2,3,4,5,6,7,8,9]}

const res=JSON.parse(json,(key,value)=>{
    // if(value && value.type ==='Buffer'){
    //     return Buffer.from(value.data);
    // }else{
    //     return value;
    // }

    return value && value.type==='Buffer'?
        Buffer.from(value.data):
            value;
});
console.log(res);//<Buffer 01 02 03 04 05 06 07 08 09>
console.log("-----------------");
const buf7=Buffer.alloc(256);
len =buf7.write('http://nodejs.cn/api/');
console.log('写入了'+len+'个字节');//写入了21个字节
console.log("-----------------");
const buf8= Buffer.alloc(265);
const len2=buf8.write('\u00bd+\u00bc=\u00be',0);
console.log(`${len2}个字节长度${buf8.toString('utf-8',0,len)}`);//8个字节长度½+¼=¾
console.log(`${len2}个字节长度${buf8.toString('utf-8',0,3)}`);//8个字节长度½+
console.log(`${len2}个字节长度${buf8.toString('utf-8')}`);//8个字节长度½+¼=¾
console.log("-----------------");
const buf9=Buffer.from([-1,5,99]);
console.log(buf9.readInt8(0));//-1
console.log(buf9.readInt8(1));//5
console.log(buf9.readInt8(2));//99
console.log("-----------------");
let buf10=Buffer.from('您好');
let buf11=Buffer.from('node');
let buf12=Buffer.concat([buf10,buf11],buf11.length+buf10.length);
console.log('buf12 内容为'+buf12.toString()+'长度为'+buf12.length);//buf12 内容为您好node长度为10
let buf13=Buffer.concat([buf10,buf11],6);
console.log('buf12 内容为'+buf13.toString()+'长度为'+buf13.length);//buf12 内容为您好长度为6
console.log("-----------------");
let copy1=Buffer.from('123456789');
let copy2=Buffer.from('AssA');
copy2.copy(copy1,1);//1AssA6789
console.log(copy1.toString());
console.log("-----------------");
let copy3=Buffer.from('123456789');
let copy4=Buffer.from('AssA');
copy4.copy(copy3,1,0,3);
console.log(copy3.toString());//1Ass56789
console.log("-----------------");
let buffer2=Buffer.from('helloWorld');
let buffer3=buffer2.slice(0,10);
console.log('buffer2:'+buffer3.toString());//buffer2:helloWorld

buffer3.write('456',0);
console.log(buffer3.toString());//456loWorld

console.log(buffer2.toString());//456loWorld
console.log("-----------------");
